<?php

namespace app\api\controller;

use app\common\BaseHelper;
use app\admin\model\Manages as ManagesMdl;
use app\admin\model\Basicdata;
use app\admin\model\Member;
use app\admin\model\Image;

use think\Db;
use think\Exception;
use think\Request;

class Manages extends BaseController
{

    /**
     * 经营主体列表
     */
    public function getList()
    {
        $param = self::getHttpParam();
        $start = 0;
        $length = 20;

        if (empty($param->townId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'townId 不能为空');
        }
        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        $where = [
            'townId' => $param->townId,
            'isDelete' => 2
        ];
        if (!empty($param->type)) {
            $where['typeId'] = $param->type;
        }
        if (isset($param->villageId) && $param->villageId) {
            $where['villageId'] = ['in', ['0', $param->villageId]];
        }

        $model = new ManagesMdl();
        $fields = 'id, name, imgUrl, typeId, categoryId, address';
        $list = $model->where($where)->field($fields)->order('isRecommend ASC')->limit($start, $length)->select();
        $total = $model->where($where)->count();

        if (empty($list)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到经营主体');
        }

        foreach ($list as &$item) {
            // 主体类型
            if($item['typeId']){
                $typeArr = get_managetype();
                $item['typeId'] = $typeArr[$item['typeId']];
            }
            // 经营类目
            if($item['categoryId']){
                $item['categoryId'] = Basicdata::where(['id' => $item['categoryId']])->value('name');
            }
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $list, $total);
    }

    /**
     * 获取详情
     */
    public function getDetail()
    {
        $param = self::getHttpParam();

        if (empty($param->townId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'townId 不能为空');
        }
        if (empty($param->manageId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'manageId 不能为空');
        }

        $manageId = $param->manageId;
        $where = [
            'townId' => $param->townId,
            'id' => $manageId
        ];

        $model = new ManagesMdl();
        $fields = '*';
        $item = $model->where($where)->field($fields)->find();
        if(empty($item)){
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到相关经营主体');
        }

        // 图集
        $imagesList = Image::where(['relatedId' => $item['id'], 'tag' => 'imglist'])->where('imgUrl', 'neq', $item['imgUrl'])->field('imgUrl')->order('sorting ASC')->select();
        $item['imagesList'] = $imagesList;
        // 主体类型
        if($item['typeId']){
            $typeArr = get_managetype();
            $item['typeId'] = $typeArr[$item['typeId']];
        }
        // 经营类目
        if($item['categoryId']){
            $item['categoryId'] = Basicdata::where(['id' => $item['categoryId']])->value('name');
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $item);
    }
}